<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:variable name="mask_dec_4" select="'#,##0.0000'"/>
<xsl:variable name="mask_dec_3" select="'#,##0.000'"/>
<xsl:variable name="mask_dec_2" select="'###,##0.00'"/>
<xsl:variable name="mask_int" select="'###,##0'"/>
<xsl:template match="/">
	<fo:root>
		<fo:layout-master-set>
			<fo:simple-page-master master-name="single_manifest"
			margin-top="0.5in" margin-bottom="0.5in" margin-left="0.5in" margin-right="0.5in">
				<fo:region-before extent="3.0in"/>
				<fo:region-body margin-top="2.5in"/>
                                <fo:region-after extent="1.7in"/>
			</fo:simple-page-master>
		</fo:layout-master-set>
		<fo:page-sequence master-name="single_manifest">
			<fo:static-content flow-name="xsl-region-before">
				<xsl:apply-templates select="Manifest"/>
			</fo:static-content>
                        <fo:static-content font-size="10pt" font-family="Times" flow-name="xsl-region-after">
                            <fo:block space-after="10pt" border-bottom-style="double" border-bottom-width="thick" border-bottom-color="black"></fo:block>
                            <fo:block space-after="10pt">
                            Payment: By D/A 30 days
                            </fo:block>
                            <fo:block space-after="10pt">
                            These commodities licenced by U.S.A. for ultimate destination.  Diversion to U.S.A is prohibited by law.
                            </fo:block>                            
                            <fo:block>
                            We certify that this commercial invoice is true and correct and that the goods described herein are of U.S.A. origin and/or manufacture.
                            </fo:block>
                            
                            <fo:block text-align="center">Page <fo:page-number/></fo:block>
                        </fo:static-content>
			<fo:flow flow-name="xsl-region-body">				
				<fo:table>
					<fo:table-column column-width="28mm"/>
					<fo:table-column column-width="50mm"/>
					<fo:table-column column-width="22mm"/>
                                        <fo:table-column column-width="28mm"/>
					<fo:table-column column-width="22mm"/>
					<fo:table-column column-width="27mm"/>
					<fo:table-body>
						<fo:table-row>
							<fo:table-cell>
								<fo:block font-weight="bold">Container ID</fo:block>
							</fo:table-cell>	
							<fo:table-cell>
								<fo:block font-weight="bold">Description</fo:block>
							</fo:table-cell>	
							<fo:table-cell>
								<fo:block font-weight="bold">Packing</fo:block>
							</fo:table-cell>
							<fo:table-cell>
								<fo:block font-weight="bold" text-align="end">Weight (lb/kg)</fo:block>
							</fo:table-cell>
							<fo:table-cell>
								<fo:block font-weight="bold" text-align="end">Unit Price</fo:block>
							</fo:table-cell>
							<fo:table-cell>
								<fo:block font-weight="bold" text-align="end">Amount</fo:block>
							</fo:table-cell>
						</fo:table-row>
						<xsl:apply-templates select="Manifest/ContainerList">
							<!-- todo: find out how to sort -->
							<!--xsl:sort data-type="String" select="Container/Id"/-->
						</xsl:apply-templates>
						<fo:table-row>
							<fo:table-cell number-columns-spanned="6">
								<fo:block text-align="end">
									<fo:leader leader-length="60mm" leader-pattern="rule"
								             rule-thickness="2pt" color="green"/>
								</fo:block>
							</fo:table-cell>
						</fo:table-row>
						
						<xsl:variable name="subtotal" select="sum(//FobAmount)"/>
						<fo:table-row>
							<fo:table-cell number-columns-spanned="4">
							</fo:table-cell>
							<fo:table-cell>
								<fo:block font-weight="bold">Subtotal:</fo:block>
							</fo:table-cell>	
							<fo:table-cell>
								<fo:block font-size="10pt" text-align="end"><xsl:value-of select="format-number($subtotal,$mask_dec_2)"/></fo:block>
							</fo:table-cell>
						</fo:table-row>
						<fo:table-row>
							<fo:table-cell number-columns-spanned="4">
							</fo:table-cell>
							<fo:table-cell>
								<fo:block font-weight="bold">Freight:</fo:block>
							</fo:table-cell>	
							<fo:table-cell>
								<fo:block font-size="10pt" text-align="end"><xsl:value-of select="format-number(//Freight,$mask_dec_2)"/></fo:block>
							</fo:table-cell>
						</fo:table-row>
						<fo:table-row>
							<fo:table-cell number-columns-spanned="4">
							</fo:table-cell>
							<fo:table-cell>
								<fo:block font-weight="bold">Trucking:</fo:block>
							</fo:table-cell>	
							<fo:table-cell>
								<fo:block font-size="10pt" text-align="end"><xsl:value-of select="format-number(//Trucking,$mask_dec_2)"/></fo:block>
							</fo:table-cell>
						</fo:table-row>
						<fo:table-row>
							<fo:table-cell number-columns-spanned="4">
							</fo:table-cell>
							<fo:table-cell>
								<fo:block font-weight="bold">Misc:</fo:block>
							</fo:table-cell>	
							<fo:table-cell>
								<fo:block font-size="10pt" text-align="end"><xsl:value-of select="format-number(//Misc,$mask_dec_2)"/></fo:block>
							</fo:table-cell>
						</fo:table-row>
						<fo:table-row>
							<fo:table-cell number-columns-spanned="4">
							</fo:table-cell>
							<fo:table-cell>
								<fo:block font-weight="bold">Total:</fo:block>
							</fo:table-cell>	
							<fo:table-cell>
								<fo:block font-size="10pt" text-align="end"><xsl:value-of select="format-number(($subtotal+//Freight+//Trucking+//Misc),$mask_dec_2)"/></fo:block>
							</fo:table-cell>
						</fo:table-row>
					</fo:table-body>
				</fo:table>
			</fo:flow>
		</fo:page-sequence>
	</fo:root>
</xsl:template>

<xsl:template match="Manifest">
   <fo:table>
     <fo:table-column column-width="3.5in"/>
     <fo:table-column column-width="3.5in"/>
     <fo:table-body>
       <fo:table-row>
         <fo:table-cell>
           <fo:block>
             <fo:external-graphic src="logo.jpg"/>
           </fo:block>
         </fo:table-cell>
         <fo:table-cell>
           <fo:block space-before="26pt" text-align="end">
             <fo:inline font-size="18pt" font-style="italic">Pioneer Metals, Inc.</fo:inline>
           </fo:block>
         </fo:table-cell>
       </fo:table-row>
       <fo:table-row>
         <fo:table-cell number-columns-spanned="2">
           <fo:block space-before="10pt" space-after="5pt" border-bottom-style="double" border-bottom-width="thick" border-bottom-color="black"></fo:block>
         </fo:table-cell>
       </fo:table-row>
       <fo:table-row>
         <fo:table-cell number-columns-spanned="2">
           <fo:block font-style="italic" font-size="9pt" text-align="end">
             3325 S. Garfield Ave. Commerce CA 90040 &#8226; (323) 888-8873 &#8226; (800) 338-9468 &#8226; FAX: (323) 888-9515
           </fo:block>
         </fo:table-cell>
       </fo:table-row>       
       <fo:table-row>
         <fo:table-cell number-columns-spanned="2">
           <fo:block space-before="16pt" space-after="10pt" text-align="center">
             <fo:inline font-size="14pt" font-weight="bold">Invoice: 
               <xsl:value-of select="JobNum"/>
             </fo:inline>
           </fo:block>
         </fo:table-cell>
       </fo:table-row>
       <fo:table-row>
         <fo:table-cell number-columns-spanned="2">
           <fo:block text-align="start">
             Vessel: <xsl:value-of select="Vessel"/>
           </fo:block>
         </fo:table-cell>
       </fo:table-row>
       <fo:table-row>
         <fo:table-cell>
           <fo:block text-align="start">
             ETD: <xsl:value-of select="Etd"/>
           </fo:block>
         </fo:table-cell>
         <fo:table-cell>
           <fo:block text-align="end">
             ETA: <xsl:value-of select="Eta"/>
           </fo:block>
         </fo:table-cell>
       </fo:table-row>
       <fo:table-row>
         <fo:table-cell number-columns-spanned="2">
           <fo:block>
             Container Notes: <xsl:value-of select="ContainerNotes"/>
           </fo:block>
         </fo:table-cell>
       </fo:table-row>
     </fo:table-body>
   </fo:table>
</xsl:template>

<xsl:template match="ContainerList">
	<xsl:for-each select="Container">
		<fo:table-row>
			<fo:table-cell>
				<fo:block font-size="10pt"><xsl:value-of select="Id"/></fo:block>
			</fo:table-cell>	
			<fo:table-cell number-columns-spanned="5">
				<!--fo:block>Nothing but ID</fo:block-->
			</fo:table-cell>
		</fo:table-row>
		<xsl:apply-templates select="ContentList"/>
	</xsl:for-each>
</xsl:template>

<xsl:template match="ContentList">
	<xsl:for-each select="Content">
		<fo:table-row>
			<fo:table-cell>
				<!--<fo:block>Nothing but details</fo:block> -->
			</fo:table-cell>	
			<fo:table-cell>
				<fo:block font-size="10pt"><xsl:value-of select="Description"/></fo:block>
			</fo:table-cell>	
			<fo:table-cell>
				<fo:block font-size="10pt"><xsl:value-of select="Remarks"/></fo:block>
			</fo:table-cell>
			<xsl:variable name="kg" select="Weight div 2.2046"/>
			<fo:table-cell>
				<fo:block font-size="10pt" text-align="end"><xsl:value-of select="format-number(Weight,$mask_int)"/> / <xsl:value-of select="format-number($kg,$mask_int)"/> </fo:block>
			</fo:table-cell>
			<fo:table-cell>
				<fo:block font-size="10pt" text-align="end"><xsl:value-of select="format-number(Cost,$mask_dec_3)"/></fo:block>
			</fo:table-cell>
			<fo:table-cell>
				<fo:block font-size="10pt" text-align="end"><xsl:value-of select="format-number(FobAmount,$mask_dec_2)"/></fo:block>
			</fo:table-cell>

		</fo:table-row>
	</xsl:for-each>
</xsl:template>

</xsl:stylesheet>
